//speichert die id Names
var myQuestionNameList = new Array();
// speichert die Reihenfolge
var myQuestionList = new Array();

// Hilfsfunktion
function getMyID(AntwortIDString)
{
	var tokenBegin = "quiz";
	var tokenEnd = "_";
	var tmpStringArray = AntwortIDString.split(tokenEnd);
	tmpStringArray = tmpStringArray[0].split(tokenBegin);
	// Die rausgefundene ID muss jetzt noch um -1 veringert werden
	var myID = parseInt( tmpStringArray[1] );
	return myID-1;
}

//Hauptfunktion
function randomQuestion() {
	myQuestionNameList = new Array();
	myQuestionList = new Array();
	generateQuestionOrder();
	changeHTMLCode();
}

//wird momentan nicht verwendet
function randomAnswer()
{
	var antwortLength = document.Antwort.length;
	for(var i=0; i < antwortLength; i++)
	{
		var radios = document.Antwort[i].radio;
		var radioLength = document.Antwort[i].radio.length;
		if(radioLength > 2) 
		{
			var randomListArray = randomList( radioLength );
			var randomRadioList = new Array();
			
			for(var y=0; y < radioLength; y++ )
			{
				randomRadioList.push( radios[y] );
			}
			
			for(var y=0; y < radioLength; y++ )
			{
				radios[y+1] = randomRadioList[randomListArray[y]];
			}
			
		}
	}
}

function randomList(arrayLength)
{
	var randomAgain = true;
	var localeZufallsZahl;
	var tmpArrayList = new Array();
	for(var x=0; x<arrayLength-1; x++)
	{
		randomAgain = true;
		while(randomAgain) {
			randomAgain = false;
			localeZufallsZahl = Math.floor((Math.random()*(arrayLength-1))+1);

			for(var i=0; i < tmpArrayList.length; i++)
			{
				if(tmpArrayList[i] == localeZufallsZahl) {
					randomAgain = true;
				}
			}
		}
		tmpArrayList.push(localeZufallsZahl);
	}
	return tmpArrayList;
}

//Alle pages zaehlen = Anzahl der Fragen
function countQuestion()
{
	var res = 0;
	var myCategory = localStorage.getItem("Category");
	for(var i=1; i <= document.Antwort.length; i++)
	{ 
		if( document.getElementById("quiz"+i+"_"+myCategory) != null )
		{
			myQuestionNameList.push( "quiz"+i+"_"+myCategory );
			res++;
		}
	}
	var tmpString = " ";
	for(var i =0; i < myQuestionNameList.length; i++)
	{
		tmpString += myQuestionNameList[i] + ", ";
	}
	return res;
}

//Zufallsgenerator
function generateQuestionOrder() 
{
	var numberOfQuestion = countQuestion();
	var randomAgain = true;
	var localeZufallsZahl;
	
	for(var x=0; x<numberOfQuestion; x++)
	{
		randomAgain = true;
		while(randomAgain) {
			randomAgain = false;
			localeZufallsZahl = Math.floor((Math.random()*numberOfQuestion));

			for(var i=0; i < myQuestionList.length; i++)
			{
				if(myQuestionList[i] == localeZufallsZahl) {
					randomAgain = true;
				}
			}
		}
		myQuestionList.push(localeZufallsZahl);
	}
	var tmpString = " ";
	for(var i =0; i < myQuestionNameList.length; i++)
	{
		tmpString += "" + myQuestionList[i] + ", ";
	}
}

//Aendern des HtmlCodes, besser gesagt der Link-Namen in der neuen Reihenfolge
function changeHTMLCode() 
{	
	//Beschriftungen der Links
	var BTN_SAVE = "Start Quiz";
	var BTN_NEXT = "Weiter";
	
	//Link-Inhalte
	var PAGE_QUIZ_START_ID = "startQuizID";
	var PAGE_QUIZ_START = "start";
	
	var PAGE_QUIZ_BASE_NAME = "quiz";
	var PAGE_QUIZ_BASE_NAME_BEGIN_ID = "quiz";
	var PAGE_QUIZ_BASE_NAME_END_ID = "LinkID";
	var PAGE_QUIZ_END = "highscore";
	
	//Anfang
	document.getElementById(PAGE_QUIZ_START_ID).href = "#"+myQuestionNameList[myQuestionList[0]];
	
	// Mitte	
	//alle Fragen:
	//for(var i=0; i < myQuestionList.length-1; i++)
	//feste Anzahl von Fragen:
	for(var i=0; i < 6; i++)
	{
		//alert(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[i]] ) + PAGE_QUIZ_BASE_NAME_END_ID);
		document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[i]] ) + PAGE_QUIZ_BASE_NAME_END_ID).href = "#"+myQuestionNameList[myQuestionList[i+1]];
		
		var changeAttribute=document.createAttribute("onclick");
		changeAttribute.nodeValue=" ";
		document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[i]] ) + PAGE_QUIZ_BASE_NAME_END_ID).removeAttribute("onclick");
		document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[i]] ) + PAGE_QUIZ_BASE_NAME_END_ID).attributes.setNamedItem(changeAttribute);		
	}

	// Ende, den letzten Link der letzten Frage aendern um auf den Highscore zu kommen	
	//document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[myQuestionList.length-1]] ) + PAGE_QUIZ_BASE_NAME_END_ID).href = "#"+PAGE_QUIZ_END;
	document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[6]] ) + PAGE_QUIZ_BASE_NAME_END_ID).href = "#"+PAGE_QUIZ_END;
		
	var changeAttribute=document.createAttribute("onclick");
	changeAttribute.nodeValue="savePoint()";
	//document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[myQuestionList.length-1]] ) + PAGE_QUIZ_BASE_NAME_END_ID).removeAttribute("onclick");
	document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[6]] ) + PAGE_QUIZ_BASE_NAME_END_ID).removeAttribute("onclick");
	//document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[myQuestionList.length-1]] ) + PAGE_QUIZ_BASE_NAME_END_ID).attributes.setNamedItem(changeAttribute);	
	document.getElementById(PAGE_QUIZ_BASE_NAME_BEGIN_ID + getMyID( myQuestionNameList[myQuestionList[6]] ) + PAGE_QUIZ_BASE_NAME_END_ID).attributes.setNamedItem(changeAttribute);	
}